﻿#region ... Copyright Notice ...
/*
   Copyright 2008 Tyler Jensen

	Author: Tyler Jensen    http://www.tsjensen.com/blog

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
#endregion

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Atrax.Contracts;
using Atrax.Library;
using Atrax.Logging;

namespace Atrax.Master
{
	internal class SpiderCallbackProcessor
	{
		//called by managed thread pool to start processing when there is a thread available
		internal void ThreadPoolCallback(object threadContext)
		{
			try
			{
				SpiderResult result = (SpiderResult)threadContext;
				RequestHandler rh = QueryController.GetRequestHandler(result.QueryId);
				if (rh != null)
				{
					rh.ProcessSpiderCallback(result);
				}
				else
				{
					//handle null - means it was timed out of the controller and removed from the list
					//must create a new request handler if possible or log failure
					try
					{
						Query query = PersistenceManager.LoadQuery(result.QueryId);
						if (query != null)  
						{
							if (query.Header.ProcessingAttempts < 3) //max of 3 attempts on query before it dies of expiration
							{
								rh = new RequestHandler();
								//send request back into handler because this one expired somehow and was not killed
								QueryResponse response = QueryController.EnqueRequest(query);
								//log that query was restarted
								Log.App(LogEvents.QueryRestartedByCallbackProcessor, "queryid=" + query.Header.QueryId + " | querytype=" + query.Header.QueryType + " | licensekey=" + query.Header.LicenseKey);
							}
							else
							{
								//exceeded processing attempts
								PersistenceManager.Remove(query.Header.QueryId);
								Log.App(LogEvents.QueryExceededProcessingAttemptsRemovedByCallbackProcessor,
									"queryid=" + query.Header.QueryId + " | querytype=" + query.Header.QueryType + " | licensekey=" + query.Header.LicenseKey);
							}
						}
						else
						{
							//log query not found but ignore the callback cuz the query was removed by being killed or completed
							Log.App(LogEvents.QueryNotFoundByCallbackProcessor, "queryid=" + query.Header.QueryId + " | querytype=" + query.Header.QueryType + " | licensekey=" + query.Header.LicenseKey); 
						}
					}
					catch (Exception ie)
					{
						Log.App(LogEvents.QueryRecallFailedInCallbackProcessor, ie);
					}
				}
			}
			catch (Exception e)
			{
				Log.App(LogEvents.CallbackProcessorFailed, e);
			}
		}
	}
}
